package com.xh.bussiness.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.crm.bean.request.ClueQueryRequest;
import com.xh.bussiness.crm.bean.request.ClueSaveRequest;
import com.xh.bussiness.crm.dao.ClueMapper;
import com.xh.bussiness.crm.model.Clue;
import com.xh.bussiness.crm.service.IClueService;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.*;
import org.springframework.stereotype.Service;

import java.util.Date;

/*
 * @Author daiwei
 * @Description
 * @Date 2019/5/24 11:07
 **/
@Service
public class ClueServiceImpl extends BaseServiceImpl<ClueMapper,Clue> implements IClueService {

    @Override
    public String addSubmitApply(ClueSaveRequest bean) {
        if(bean==null){
            return "参数不正确";
        }
        if(StringUtils.isEmpty(bean.getSchoolName())){
            return "学校信息不能为空";
        }
        if(StringUtils.isEmpty(bean.getApplierName())){
            return "校长名字信息不能为空";
        }
        if(StringUtils.isEmpty(bean.getApplierMobile())){
            return "校长手机信息不能为空";
        }
        if(!RegexUtils.isMobile(bean.getApplierMobile().trim())){
            return "手机号格式并不正确，请输入11位手机号";
        }

        Clue clue=new Clue();
        ObjectUtils.copyNonNullProperties(bean,clue);
        clue.setStatus(0);
        this.insert(clue);
        return null;
    }

    @Override
    public String addSubmitApplyCheck(ClueSaveRequest bean) {
        if(bean==null){
            return "参数不正确";
        }
        if(StringUtils.isEmpty(bean.getSchoolName())){
            return "学校信息不能为空";
        }
        if(StringUtils.isEmpty(bean.getApplierName())){
            return "校长名字信息不能为空";
        }
        if(StringUtils.isEmpty(bean.getApplierMobile())){
            return "校长手机信息不能为空";
        }
        if(!RegexUtils.isMobile(bean.getApplierMobile().trim())){
            return "手机号格式并不正确，请输入11位手机号";
        }
        if(!StringUtils.isEmpty(bean.getSmsCode())){
            return "手机验证码不能为空";
        }

        String checkCode=(String)CacheUtils.get("checkCode_" + bean.getApplierMobile());
        if(!bean.getSmsCode().equals(checkCode)){
            return "手机验证码不正确";
        }

        Clue clue=new Clue();
        ObjectUtils.copyNonNullProperties(bean,clue);
        clue.setStatus(0);
        this.insert(clue);
        return null;
    }

    @Override
    public XhPage queryYestodayPageList(XhPage page,ClueQueryRequest bean) {
        QueryWrapper qw=new QueryWrapper();
        qw.lt("create_time",DateUtils.getTodayDate());
        qw.ge("create_time", DateUtils.dateAddForDay(DateUtils.getTodayDate(),-1));
        return (XhPage)this.selectPage(page,qw,Clue.class);
    }

}
